import 'package:flutter/material.dart';

///@author: slc
///主题拓展

/// 定义一个自定义的 ThemeExtension 类 SlcTidyUpColor，用于扩展主题颜色
class SlcTidyUpColor extends ThemeExtension<SlcTidyUpColor> {

  //下面这些颜色其实不推荐使用，应该是用ThemeDate中的相关MD颜色，但是国内大部分app设计规范不符合MD规范，所以下面暂时保留后续可能用的上

  //全局默认第二颜色字体高亮
  final Color globalSecondaryTextColorWhite;

  //全局默认第二颜色字体暗黑
  final Color globalSecondaryTextColorBlack;

  //全局默认第三颜色字体高亮
  final Color globalHintTextColorWhite;

  //全局默认第三颜色字体暗黑
  final Color globalHintTextColorBlack;

  //全局默认分轨器高亮
  final Color globalDividerColorWhite;

  //全局默认第三颜色字体暗黑
  final Color globalDividerColorBlack;

  SlcTidyUpColor(
      {this.globalSecondaryTextColorWhite = Colors.white70,
      this.globalSecondaryTextColorBlack = Colors.black54,
      this.globalHintTextColorWhite = Colors.white38,
      this.globalHintTextColorBlack = Colors.black38,
      this.globalDividerColorWhite = Colors.white12,
      this.globalDividerColorBlack = Colors.black12});

  //根据主题获取第一字体颜色
  Color getTextColorPrimaryByTheme(ThemeData themeData) {
    return themeData.colorScheme.onSurface;
  }

  //根据主题获取第一字体颜色
  Color getTextColorPrimaryByDark(bool isDark) {
    throw new Exception("暂未实现");
  }

  //根据主题获取第二字体颜色
  Color getTextColorSecondaryByTheme(ThemeData themeData) {
    return themeData.colorScheme.onSurfaceVariant;
  }

  //根据主题获取提示字体颜色
  Color getTextColorSecondaryByDark(bool isDark) {
    throw new Exception("暂未实现");
  }

  //根据主题获取第三字体颜色
  Color getTextColorHintByTheme(ThemeData themeData) {
    return themeData.colorScheme.onSurfaceVariant.withValues(alpha: 0.6);
  }

  //根据主题获取第三字体颜色
  Color getTextColorHintByDark(bool isDark) {
    throw new Exception("暂未实现");
  }

  //根据主题获取分轨器颜色
  //此处需要完善，需要知道系统是怎么默认的
  Color getDividerColorByTheme(ThemeData themeData) {
    return getDividerColorByDark(themeData.brightness == Brightness.dark);
  }

  //根据主题获取分轨器颜色
  //此处需要完善，需要知道系统是怎么默认的
  Color getDividerColorByDark(bool isDark) {
    if (isDark) {
      return globalDividerColorWhite;
    } else {
      return globalDividerColorBlack;
    }
  }

  //获取card颜色，策略和Card组件的策略差不多
  Color getCardColor(BuildContext context) {
    return getCardColorByTheme(Theme.of(context));
  }

  //获取card颜色，策略和Card组件的策略差不多
  Color getCardColorByTheme(ThemeData themeData) {
    final CardThemeData cardTheme = themeData.cardTheme;
    if (cardTheme.color != null) {
      return cardTheme.color!;
    }
    if (themeData.useMaterial3) {
      return themeData.colorScheme.surfaceContainerLow;
    }
    return themeData.cardColor;
  }

  @override
  ThemeExtension<SlcTidyUpColor> copyWith() {
    return SlcTidyUpColor(
        globalSecondaryTextColorBlack: globalSecondaryTextColorBlack,
        globalSecondaryTextColorWhite: globalSecondaryTextColorWhite,
        globalHintTextColorBlack: globalHintTextColorBlack,
        globalHintTextColorWhite: globalHintTextColorWhite,
        globalDividerColorBlack: globalDividerColorBlack,
        globalDividerColorWhite: globalDividerColorWhite);
  }

  @override
  ThemeExtension<SlcTidyUpColor> lerp(covariant ThemeExtension<SlcTidyUpColor>? other, double t) {
    if (other is! SlcTidyUpColor) {
      return this;
    }
    return SlcTidyUpColor(
        globalSecondaryTextColorBlack:
            Color.lerp(globalSecondaryTextColorBlack, other.globalSecondaryTextColorBlack, t)!,
        globalSecondaryTextColorWhite:
            Color.lerp(globalSecondaryTextColorWhite, other.globalSecondaryTextColorWhite, t)!,
        globalHintTextColorBlack:
            Color.lerp(globalHintTextColorBlack, other.globalHintTextColorBlack, t)!,
        globalHintTextColorWhite:
            Color.lerp(globalHintTextColorWhite, other.globalHintTextColorWhite, t)!,
        globalDividerColorBlack:
            Color.lerp(globalDividerColorBlack, other.globalDividerColorBlack, t)!,
        globalDividerColorWhite:
            Color.lerp(globalDividerColorWhite, other.globalDividerColorWhite, t)!);
  }
}

/// 定义一个自定义的 ThemeExtension 类 SlcListTileStyle，用于扩展列表项样式
class SlcListTileStyle extends ThemeExtension<SlcListTileStyle> {

  TextStyle getItemTitleStyleByContext(BuildContext context) {
    return getItemTitleStyleByThemeData(Theme.of(context));
  }

  TextStyle getItemTitleStyleByThemeData(ThemeData themeData) {
    return themeData.listTileTheme.titleTextStyle ??
        themeData.textTheme.bodyLarge!.copyWith(color: themeData.colorScheme.onSurface);
  }

  TextStyle getItemSubTitleStyleByContext(BuildContext context) {
    return getItemSubTitleStyleByThemeData(Theme.of(context));
  }

  TextStyle getItemSubTitleStyleByThemeData(ThemeData themeData) {
    return themeData.listTileTheme.subtitleTextStyle ??
        themeData.textTheme.bodyMedium!.copyWith(color: themeData.colorScheme.onSurfaceVariant);
  }

  @override
  ThemeExtension<SlcListTileStyle> copyWith() {
    return new SlcListTileStyle();
  }

  @override
  ThemeExtension<SlcListTileStyle> lerp(covariant ThemeExtension<SlcListTileStyle>? other, double t) {
    if (other is! SlcListTileStyle) {
      return this;
    }
    return new SlcListTileStyle();
  }
}

/// 定义一个自定义的 ThemeExtension 类 SlcTidyUpStyle，用于扩展主题样式
class SlcTidyUpStyle extends ThemeExtension<SlcTidyUpStyle> {

  //基础控件
  late final Divider defLightDivider;
  late final Divider defDarkDivider;

  SlcTidyUpColor? _defSlcTidyUpColor;

  //安全的获取getSlcTidyUpColor
  SlcTidyUpColor getSlcTidyUpColor(ThemeData themeData) {
    SlcTidyUpColor? slcTidyUpColor = themeData.extensions[SlcTidyUpColor] as SlcTidyUpColor?;
    if (slcTidyUpColor != null) {
      return slcTidyUpColor;
    }
    if (_defSlcTidyUpColor == null) {
      _defSlcTidyUpColor = SlcTidyUpColor();
    }
    return _defSlcTidyUpColor!;
  }

  SlcTidyUpStyle(this.defLightDivider, this.defDarkDivider);

  SlcTidyUpStyle.fast(Color lightDividerColor, Color darkDividerColor) {
    defLightDivider = Divider(height: 0.8, thickness: 0.8, color: lightDividerColor);
    defDarkDivider = Divider(height: 0.8, thickness: 0.8, color: darkDividerColor);
  }

  //获取标题栏textTitle
  TextStyle? getTitleTextStyle(ThemeData themeData) {
    TextStyle? textStyle = themeData.appBarTheme.titleTextStyle;
    if (textStyle == null) {
      textStyle = themeData.textTheme.titleLarge;
    }
    return textStyle;
  }

  //获取标题栏toolbar title
  TextStyle? getToolbarTextStyle(ThemeData themeData) {
    TextStyle? textStyle = themeData.appBarTheme.toolbarTextStyle;
    if (textStyle == null) {
      textStyle = themeData.textTheme.bodyMedium;
    }
    return textStyle;
  }

  Divider getDefDividerByTheme(ThemeData themeData) {
    return getDefDividerByDark(themeData.brightness == Brightness.dark);
  }

  Divider getDefDividerByDark(bool isDark) {
    return isDark ? defDarkDivider : defLightDivider;
  }

  //下面这些颜色其实不推荐使用，应该是用ThemeDate中的相关MD颜色，但是国内大部分app设计规范不符合MD规范，所以下面暂时保留后续可能用的上

  //根据主题获取第一字体颜色
  TextStyle? getTextColorPrimaryStyleByTheme(ThemeData themeData) {
    return themeData.textTheme.bodyLarge
        ?.copyWith(color: getSlcTidyUpColor(themeData).getTextColorPrimaryByTheme(themeData));
  }

  //根据主题获取第二字体颜色
  TextStyle getTextColorPrimaryStyleByDark(bool isDark) {
    throw new Exception("暂未实现");
  }

  //根据主题获取第二字体颜色
  TextStyle? getTextColorSecondaryStyleByTheme(ThemeData themeData) {
    return themeData.textTheme.bodyMedium
        ?.copyWith(color: getSlcTidyUpColor(themeData).getTextColorSecondaryByTheme(themeData));
  }

  //根据主题获取第二字体颜色
  TextStyle getTextColorSecondaryStyleByDark(bool isDark) {
    throw new Exception("暂未实现");
  }

  //根据主题获取第三字体颜色
  TextStyle? getTextColorHintStyleByTheme(ThemeData themeData) {
    return themeData.textTheme.bodyMedium
        ?.copyWith(color: getSlcTidyUpColor(themeData).getTextColorHintByTheme(themeData));
  }

  //根据主题获取第三字体颜色
  TextStyle getTextColorHintStyleByDark(bool isDark) {
    throw new Exception("暂未实现");
  }

  //根据主题获取LabelTextStyle
  TextStyle? getLabelFloatTextStyleByTheme(ThemeData themeData) {
    return themeData.textTheme.bodySmall
        ?.copyWith(color: getSlcTidyUpColor(themeData).getTextColorHintByTheme(themeData));
  }

  //根据主题获取LabelTextStyle
  TextStyle getLabelFloatTextStyleByDark(bool isDark) {
    throw new Exception("暂未实现");
  }

  @override
  ThemeExtension<SlcTidyUpStyle> copyWith() {
    return SlcTidyUpStyle(this.defLightDivider, this.defDarkDivider);
  }

  @override
  ThemeExtension<SlcTidyUpStyle> lerp(covariant ThemeExtension<SlcTidyUpStyle>? other, double t) {
    if (other is! SlcTidyUpStyle) {
      return this;
    }
    return SlcTidyUpStyle.fast(
        Color.lerp(this.defLightDivider.color, other.defLightDivider.color, t)!,
        Color.lerp(this.defDarkDivider.color, other.defDarkDivider.color, t)!);
  }
}

/// 定义一个扩展方法，用于获取直接根据ThemeData获取主题
extension SlcThemeDataExtensions on ThemeData {
  SlcTidyUpColor get slcTidyUpColor {
    return this.extensions[SlcTidyUpColor] as SlcTidyUpColor;
  }

  SlcListTileStyle get slcListTileStyle {
    return this.extensions[SlcListTileStyle] as SlcListTileStyle;
  }

  SlcTidyUpStyle get slcTidyUpStyle {
    return this.extensions[SlcTidyUpStyle] as SlcTidyUpStyle;
  }
}
